class SignalingService {
  constructor() {
    this.socket = null;
    this.onMessageCallback = null;
    this.onUserJoinedCallback = null;
    this.onUserLeftCallback = null;
  }

  connect(roomId) {
    // 这里使用 WebSocket 连接信令服务器
    this.socket = new WebSocket(`ws://your-signaling-server.com/ws?room=${roomId}`);

    this.socket.onopen = () => {
      console.log('Connected to signaling server');
    };

    this.socket.onmessage = (event) => {
      const message = JSON.parse(event.data);
      
      switch (message.type) {
        case 'user-joined':
          if (this.onUserJoinedCallback) {
            this.onUserJoinedCallback(message.userId);
          }
          break;
        case 'user-left':
          if (this.onUserLeftCallback) {
            this.onUserLeftCallback(message.userId);
          }
          break;
        case 'offer':
        case 'answer':
        case 'ice-candidate':
          if (this.onMessageCallback) {
            this.onMessageCallback(message);
          }
          break;
      }
    };

    this.socket.onerror = (error) => {
      console.error('WebSocket error:', error);
    };

    this.socket.onclose = () => {
      console.log('Disconnected from signaling server');
    };
  }

  disconnect() {
    if (this.socket) {
      this.socket.close();
      this.socket = null;
    }
  }

  sendMessage(message) {
    if (this.socket && this.socket.readyState === WebSocket.OPEN) {
      this.socket.send(JSON.stringify(message));
    }
  }

  onMessage(callback) {
    this.onMessageCallback = callback;
  }

  onUserJoined(callback) {
    this.onUserJoinedCallback = callback;
  }

  onUserLeft(callback) {
    this.onUserLeftCallback = callback;
  }
}

export default new SignalingService(); 